<script setup>
import { reactive, onMounted, onBeforeUnmount } from 'vue';
import { getAllSensorData } from '@/api/sensor';

const config = reactive({
  header: ['数据种类', '数值', '单位'],
  data: [],
  index: true,
  columnWidth: [50],
  align: ['center'],
  headerBGC: 'rgba(0, 28, 36, 0.8)',
  oddRowBGC: 'rgba(0, 28, 36, 0.4)',
  evenRowBGC: 'rgba(0, 28, 36, 0.3)',
  waitTime: 3000,
  hoverPause: true,
});

// 更新数据
const updateData = async () => {
  try {
    const data = await getAllSensorData();
    config.data = data;
  } catch (error) {
    console.error('更新数据失败:', error);
  }
};

// 组件挂载时开始定时更新
onMounted(() => {
  // 立即更新一次数据
  updateData();
  // 每分钟更新一次数据
  const timer = setInterval(updateData, 60000);

  // 组件卸载时清除定时器
  onBeforeUnmount(() => {
    clearInterval(timer);
  });
});

const mouseoverHandler = (e) => {
  console.log(e);
};

const clickHandler = (e) => {
  console.log(e);
};
</script>

<template>
  <div class="right-center">
    <dv-border-box-9 :color="['#00ffd9', 'rgba(0, 255, 217, 0.3)']" class="box">
      <div class="content-wrapper">
        <div class="header">
          <div class="title-container">
            <dv-decoration3 class="decoration-left" :color="['#00ffd9', 'rgba(0, 255, 217, 0.3)']" />
            <dv-border-box-2 class="title-box" :color="['#00ffd9', 'rgba(0, 255, 217, 0.3)']">
              <div class="title">呈贡区大棚实验基地数据</div>
            </dv-border-box-2>
            <dv-decoration3 class="decoration-right" :color="['#00ffd9', 'rgba(0, 255, 217, 0.3)']" />
          </div>
        </div>

        <div>
          <dv-scroll-board 
            :config="config" 
            style="width:460px;height:220px" 
            @mouseover="mouseoverHandler" 
            @click="clickHandler" 
          />
        </div>
      </div>
    </dv-border-box-9>
  </div>
</template>

<style scoped>
.box {
  width: 485px;
  height: 290px;
  background: rgba(0, 28, 36, 0.3);
}
.right-center {
  width: 100%;
  height: 100%;
}

.content-wrapper {
  width: 100%;
  height: 100%;
  padding: 16px;
  border-radius: 4px;
  background: rgba(0, 28, 36, 0.5);
}

.header {
  width: 100%;
}

.title-container {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.title-box {
  width: fit-content;
  height: 40px;
  padding: 0 30px;
}

.title {
  color: #00ffd9;
  font-size: 16px;
  font-weight: bold;
  white-space: nowrap;
  padding: 0 10px;
  line-height: 40px;
  text-shadow: 0 0 10px rgba(0, 255, 217, 0.3);
}

.decoration-left,
.decoration-right {
  width: 90px;
  height: 20px;
}

:deep(.dv-scroll-board) {
  --header-color: #00ffd9;
  --text-color: #00ffd9;
  --border-color: rgba(0, 255, 217, 0.3);
  --hover-bg: rgba(0, 255, 217, 0.1);
}
</style>